package string;

import java.util.Arrays;

/**
 * 字符串支持正则表达式方法二
 * String[] split(String regex)
 * 将当前字符串中按照满足正则表达式的部分进行拆分
 */
public class SplitDemo {
    public static void main(String[] args) {
        String line = "abc123def456ghi";

        String[] data = line.split("[0-9]+");
        System.out.println(data.length);
        System.out.println(Arrays.toString(data));

        //按照确定字符拆分
        line = "abc,def,ghi";
        data = line.split(",");
        System.out.println(data.length);
        System.out.println(Arrays.toString(data));
        /*
            特殊点:
            1:如果字符串最开始就是可拆分项,那么会在之前拆分出一个空字符串
            2:如果有两个连续的可拆分项,那么它们中间会拆分出一个空字符串
            3:如果是字符串末尾连续出现可拆分项,那么拆分出的空字符串会被忽略
         */
        line = ",abc,,,def,ghi,,,,,,,,,,,,,,,,,,,,,,,,,";
        data = line.split(",");
        System.out.println(data.length);
        System.out.println(Arrays.toString(data));


        line = "abc.def.ghi";
        data = line.split("\\.");
        System.out.println(data.length);
        System.out.println(Arrays.toString(data));

        line = "abc\\def\\ghi";
        System.out.println(line);
        data = line.split("\\\\");
        System.out.println(data.length);
        System.out.println(Arrays.toString(data));

    }
}




